part of 'sounds_button.dart';

class PolymerData {
  PolymerData(this.controller, this.data);

  final SoundsRecorderController controller;

  final RecordingMaskOverlayData data;
}

class PolymerState extends InheritedWidget {
  const PolymerState({
    super.key,
    required this.data,
    required super.child,
  });

  final PolymerData data;

  static PolymerData of(BuildContext context) {
    final scope = context.dependOnInheritedWidgetOfExactType<PolymerState>();
    return scope!.data;
  }

  @override
  bool updateShouldNotify(covariant PolymerState oldWidget) {
    return oldWidget.data != data;
  }
}

const _duration = Duration(milliseconds: 220);

class RecordingStatusMaskView extends StatelessWidget {
  const RecordingStatusMaskView(
    this.polymerData, {
    super.key,
    this.onCancelSend,
    this.onVoiceSend,
    this.onTextSend,
  });

  final PolymerData polymerData;

  final VoidCallback? onCancelSend;

  final VoidCallback? onVoiceSend;

  final VoidCallback? onTextSend;

  @override
  Widget build(BuildContext context) {
    final paddingSide =
        (Get.width - polymerData.data.iconFocusSize * 3) / 3;

    final data = polymerData.data;

    return Material(
      color: Colors.black.withOpacity(0.7),
      child: PolymerState(
        data: polymerData,
        child: ValueListenableBuilder(
          valueListenable: polymerData.controller.status,
          builder: (context, value, child) {
            return _MaskStackView(
              children: [
                _Bubble(
                  paddingSide: paddingSide,
                ),
                Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Text(value.title, style: data.maskTxtStyle),
                    const SizedBox(height: 28),
                    CustomPaint(
                      painter: _RecordingPainter(
                          value == SoundsMessageStatus.recording),
                      child: Container(
                        width: double.infinity,
                        height: data.sendAreaHeight,
                        alignment: Alignment.center,
                        child: VoiceIcon(color: data.iconTxtColor),
                      ),
                    ),
                  ],
                ),
              ],
            );
          },
        ),
      ),
    );
  }
}

class VoiceIcon extends StatelessWidget {
  const VoiceIcon({
    super.key,
    this.color,
    this.size,
  });

  final Color? color;

  final double? size;

  @override
  Widget build(BuildContext context) {
    return Image.asset(
        "assets/images/home11.png",
        width: 62,
        height: 62,
    );
  }
}

class _MaskStackView extends StatelessWidget {
  const _MaskStackView({
    required this.children,
  });

  final List<Widget> children;

  @override
  Widget build(BuildContext context) {
    final polymerState = PolymerState.of(context);

    return GestureDetector(
      onTap: () {
        FocusScope.of(context).unfocus();
      },
      child: Stack(alignment: Alignment.bottomCenter, children: [
        Positioned(
          child: Container(
            decoration: const BoxDecoration(
                gradient: LinearGradient(
              begin: Alignment.bottomCenter,
              end: Alignment.topCenter,
              colors: [
                Color(0xFF474747),
                Color(0x00474747),
              ],
            )),
          ),
        ),
        Positioned(
          child: Container(
            height: polymerState.data.sendAreaHeight +
                polymerState.data.iconFocusSize,
            decoration: const BoxDecoration(
                gradient: LinearGradient(
              begin: Alignment.bottomCenter,
              end: Alignment.topCenter,
              colors: [
                Color(0xFF474747),
                Color(0x22474747),
              ],
            )),
          ),
        ),
        ...children,
      ]),
    );
  }
}


class _Bubble extends StatelessWidget {
  const _Bubble({
    required this.paddingSide,
  });

  final double paddingSide;
  

  @override
  Widget build(BuildContext context) {
    final polymerState = PolymerState.of(context);

    final data = polymerState.data;
    final status = polymerState.controller.status.value;

    const height = 84.0;

    Rect rect = const Rect.fromLTRB(24, 0, 24, height);

    if (status == SoundsMessageStatus.recording) {
      rect = Rect.fromLTRB(paddingSide + data.iconFocusSize / 2, 0,
          paddingSide + data.iconFocusSize / 2, height);
    } else if (status == SoundsMessageStatus.canceling) {

      rect = Rect.fromLTRB(paddingSide + data.iconFocusSize / 2, 0,
          paddingSide + data.iconFocusSize / 2, height);
    }

    double bottom = 0;

    final keyboardHeight = MediaQuery.of(context).viewInsets.bottom;

    return Positioned(
      left: 0,
      right: 0,
      bottom:
          max(keyboardHeight, data.sendAreaHeight * 2 + data.iconFocusSize) +
              20,
      child: AnimatedContainer(
        duration: _duration,
        curve: Curves.easeInOut,
        margin: EdgeInsets.only(left: rect.left, right: rect.right, bottom: 0),
        constraints: BoxConstraints(
          minHeight: rect.height + bottom,
          maxHeight: (rect.height + bottom) * 2,
          maxWidth: rect.width,
          minWidth: rect.width,
        ),
        child: RepaintBoundary(
          child:
          // Container(
          //   decoration: BoxDecoration(
          //     image: DecorationImage(
          //       image: AssetImage(R.assetsImagesHome10),
          //       //fit: BoxFit.cover,
          //     ),
          //   ),
          //   padding: const EdgeInsets.only(
          //       left: 10, right: 10, top: 0, bottom: 10),
          //   child:  AmpContent(status:status),
          // ),
          CustomPaint(
            painter: _BubblePainter(data, status, paddingSide),
            child: Container(
              padding: const EdgeInsets.only(
                  left: 10, right: 10, top: 10, bottom: 10),
              child:  AmpContent(status:status),
            ),
          ),
        ),
      ),
    );
  }
}

class AmpContent extends StatelessWidget {

  final SoundsMessageStatus status;
  const AmpContent({super.key, required this.status});

  @override
  Widget build(BuildContext context) {
    final polymerState = PolymerState.of(context);
    return CustomPaint(
      painter: WavePainter(polymerState.controller.amplitudeList,status),
    );
  }
}

